<?php include "../public/header.php";?>
<?php include "../public/header_edit.php";?>
<style>
    body::-webkit-scrollbar {width: 0;}
    .shuo{background-color: #F6F6F6;color: #999999;padding: 10px 0}
    .shuo p{padding: 0 10px}
    .add-kuai-btn{text-align: center;padding: 20px;background-color: #FFFFFF}
    .add-kuai-btn i{margin-right: 5px}
    .change_page .divkuai .tablediv{overflow: visible}
    .change_page .divkuai table .opt_width{width: 100%}
    .short_table_list table .xuhao{min-width: 10px!important;}
    text{color: red}

</style>
<body id="optPage">
<form class="layui-form layui-form-pane" id="commentForm">
    <div class="page-title"><div class="page-title-con"><span>编辑条件</span><div class="move_title"></div><?php include "../public/sub.php";?></div></div>
    <div class="main-container">
        <div class="block-first">
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <div class="shuo">
                            <p>• 审批节点条件为‘或’关系，不支持并行审批，当满足条件就会流程就会进入到对应满足条件的审批节点。</p>
                            <p>• 可通过设置条件优先级，实现流程走向的优先级判断；数值越小，优先级越高，当优先级相同时则会随机选择一个条件进行判断。 </p>
                            <p>• 当前置节点为人工节点时，不可设置流转条件，条件优先级将影响节点显示顺序（数字越小排序越靠前）。</p>
                        </div>
                    </div>
                    <div class="layui-col-md12">
                        <label class="layui-form-label"><text>*</text>优先级</label>
                        <div class="layui-input-block">
                            <select id="level" name="level">
                                <option value="1">1</option>
                                <option value="2">2</option>
                                <option value="3">3</option>
                                <option value="4">4</option>
                                <option value="5">5</option>
                            </select>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="kuailistdiv">

        </div>



        <div class="add-kuai-btn">
            <button id="addkuai" class="layui-btn layui-btn-self-hui" type="button"><i class="fa fa-plus"></i>添加条件快</button>
        </div>
    </div>
</form>
<script>
    var param_sectionnum = getQueryVariable('sectionnum');
    var param_classid = getQueryVariable('classid');


    var factorArr=[],conderArr=[],kuainum=1;
    layui.use(['laytp', 'form', 'table'], function() {
        var form = layui.form, layer = layui.layer,table = layui.table;

        var level = 1;
        var list = [];
        var param_domvalue = $('section[key="'+param_sectionnum+'"] .nonedata', parent.document).text() || '';
        if(param_domvalue){
            var valueObj = JSON.parse(param_domvalue);
            level = valueObj.level;
            list = valueObj.list;
        }
        facade.ajax({type: 'get', url: ask_flow_class_assist,data:{classid:param_classid}}).done(function(res){
            if (res.code === 2000) {
                var data = res.data;
                var factorArr = res.data.factor;
                var conderArr = res.data.conder;

                $('#level').val(level);

                if(list.length < 1){
                    str_addkuai(factorArr, conderArr, []);
                }else{
                    for (var key in list){
                        str_addkuai(factorArr, conderArr, list[key]);
                    }
                }

                $("#addkuai").show();
                $("#addkuai").click(function () {
                    str_addkuai(factorArr, conderArr, []);
                });

                render_from();
            }
        });

        form.on('submit(add)', function(data){
            var data = data.field;
            var levelname = '优先级'+data.level;

            var valuename = ''
            var list = [];
            var kuainum = 1;
            $(".listerror").hide();
            try {
                $('.short_table_tbody').each(function(){
                    kuainum = $(this).parents('.short_table_list').attr('key');
                    var __list = [];
                    var __valuename = '';

                    var hangNum = 1;
                    $(this).children('tr').each(function() {
                        var hangName = '行'+hangNum;
                        var fieldname = $(this).find('.fieldname').val();
                        var fieldnameText = $(this).find('.fieldname>option:selected').text();
                        var fieldtype = $(this).find('.fieldtype').val();
                        var fieldtypeText = $(this).find('.fieldtype>option:selected').text();
                        var fieldvalue = $(this).find('.fieldvalue').val();
                        if(!fieldname && !fieldtype){
                            return true;
                        }else{
                            if(!fieldname){
                                throw new Error('条件块'+kuainum+hangName+"请选择条件字段");
                            }
                            if(!fieldtype){
                                throw new Error('条件块'+kuainum+hangName+"请选择条件类型");
                            }
                            if(__valuename){
                                __valuename += '，'+fieldnameText+' '+fieldtypeText+' '+fieldvalue
                            }else{
                                __valuename += fieldnameText+' '+fieldtypeText+' '+fieldvalue
                            }

                            __list.push({
                                fieldname: fieldname,
                                fieldtype: fieldtype,
                                fieldvalue: fieldvalue,
                            });
                        }
                    })
                    if(__list.length > 0){
                        list.push(__list);
                    }
                    if(valuename){
                        valuename += '<br/>或<br/>'+__valuename
                    }else{
                        valuename += __valuename
                    }
                });
            } catch(e) {
                $(".listerror"+kuainum).find('span').text(e.message);
                $(".listerror"+kuainum).show();
                return false;
            }
            data.list = list;


            if(!valuename){
                valuename = '无条件';
            }

            var datastr = JSON.stringify(data);
            $('section[key="'+param_sectionnum+'"] .levelname', parent.document).text(levelname)
            $('section[key="'+param_sectionnum+'"] .valuename', parent.document).html(valuename)
            $('section[key="'+param_sectionnum+'"] .nonedata', parent.document).text(datastr)
            layer_close();
            return false;
        });

    });



    function str_addkuai(factorArr, conderArr, mingxi) {
        var str = '';
        str += '<div class="block-generic">';
        str += '<div class="layui-form-item height_auto">';
        str += '<div class="short_table_list" key="'+kuainum+'">';
        str += '<div class="opt-btn-group">';
        str += '<div><span>条件块'+kuainum+'</span></div>';
        str += '<div>';
        str += '<button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_add_row">插入行</button>';
        str += '<button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_row">删除行</button>';
        str += '<button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_all">删除全部</button>';
        str += '<button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn" onclick="del_kuai(this)">删除块</button>';
        str += '</div>';
        str += '</div>';
        str += '<table class="layui-table" lay-size="sm">';
        str += '<thead>';
        str += '<tr>';
        str += '<th style="width: 45px">序号</th>';
        str += '<th><text>*</text>条件字段</th>';
        str += '<th><text>*</text>条件类型</th>';
        str += '<th>条件值</th>';
        str += '</tr>';
        str += '</thead>';
        str += '<tbody class="short_table_tbody">';

        if(mingxi.length > 0){
            for (var ii in mingxi){
                var xuhao = parseInt(ii)+1;
                str += '<tr>';
                str += '<td style="width: 45px">'+xuhao+'</td>';
                str += '<td>';
                str += '<select class="fieldname">';
                str += '<option value="">请选择</option>';
                for(var key in factorArr){
                    if(factorArr[key].name == mingxi[ii].fieldname){
                        str += '<option value="'+factorArr[key].name+'" selected>'+factorArr[key].title+'</option>';
                    }else{
                        str += '<option value="'+factorArr[key].name+'">'+factorArr[key].title+'</option>';
                    }
                }
                str += '</select>';
                str += '</td>';
                str += '<td>';
                str += '<select class="fieldtype">';
                str += '<option value="">请选择</option>';
                for(var key in conderArr){
                    if(key == mingxi[ii].fieldtype){
                        str += '<option value="'+key+'" selected>'+conderArr[key]+'</option>';
                    }else{
                        str += '<option value="'+key+'">'+conderArr[key]+'</option>';
                    }
                }
                str += '</select>';
                str += '</td>';
                str += '<td><input class="layui-input fieldvalue" value="'+mingxi[ii].fieldvalue+'"></td>';
                str += '</tr>';
            }
        }else{
            str += '<tr>';
            str += '<td style="width: 45px">1</td>';
            str += '<td>';
            str += '<select class="fieldname">';
            str += '<option value="">请选择</option>';
            for(var key in factorArr){
                str += '<option value="'+factorArr[key].name+'">'+factorArr[key].title+'</option>';
            }
            str += '</select>';
            str += '</td>';
            str += '<td>';
            str += '<select class="fieldtype">';
            str += '<option value="">请选择</option>';
            for(var key in conderArr){
                str += '<option value="'+key+'">'+conderArr[key]+'</option>';
            }
            str += '</select>';
            str += '</td>';
            str += '<td><input class="layui-input fieldvalue"></td>';
            str += '</tr>';
        }

        str += '</tbody>';
        str += '</table>';
        str += '<div class="listerror listerror'+kuainum+'">提示：<span></span></div>';
        str += '</div>';
        str += '</div>';
        str += '</div>';
        $(".kuailistdiv").append(str);
        str_tablist(kuainum, factorArr, conderArr);
        kuainum++;
    }


    function str_tablist(kuainum, factorArr, conderArr) {
        var str = '';
        str += '<tr>';
        str += '<td style="width: 45px">1</td>';
        str += '<td>';
        str += '<select class="fieldname">';
        str += '<option value="">请选择</option>';
        for(var key in factorArr){
            str += '<option value="'+factorArr[key].name+'">'+factorArr[key].title+'</option>';
        }
        str += '</select>';
        str += '</td>';
        str += '<td>';
        str += '<select class="fieldtype">';
        str += '<option value="">请选择</option>';
        for(var key in conderArr){
            str += '<option value="'+key+'">'+conderArr[key]+'</option>';
        }
        str += '</select>';
        str += '</td>';
        str += '<td><input class="layui-input fieldvalue"></td>';
        str += '</tr>';
        short_table_opt_func(str,kuainum);
        render_from();
    }



    function del_kuai(obj) {
        $(obj).parents('.block-generic').remove();
    }

</script>
</body>
</html>
